package com.logictech.activity.system.web.back;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.VelocityContext;
import org.jeecgframework.p3.core.common.utils.AjaxJson;
import org.jeecgframework.p3.core.util.SystemTools;
import org.jeecgframework.p3.core.util.plugin.ViewVelocity;
import org.jeecgframework.p3.core.utils.common.PageQuery;
import org.jeecgframework.p3.core.utils.common.StringUtils;
import org.jeecgframework.p3.core.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.logictech.activity.system.entity.SystemAuth;
import com.logictech.activity.system.entity.MenuFunction;
import com.logictech.activity.system.service.SystemAuthService;
import com.logictech.activity.system.util.SystemUtil;

@Controller
@RequestMapping({ "/system/back/systemAuth" })
public class SystemAuthController extends BaseController {
	@Autowired
	private SystemAuthService systemAuthService;

	@RequestMapping(value = { "list" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET,
			org.springframework.web.bind.annotation.RequestMethod.POST })
	public void list(@ModelAttribute SystemAuth query, HttpServletResponse response, HttpServletRequest request,
			@RequestParam(required = false, value = "pageNo", defaultValue = "1") int pageNo,
			@RequestParam(required = false, value = "pageSize", defaultValue = "100") int pageSize) throws Exception {
		PageQuery<SystemAuth> pageQuery = new PageQuery<SystemAuth>();
		pageQuery.setPageNo(pageNo);
		pageQuery.setPageSize(pageSize);
		VelocityContext velocityContext = new VelocityContext();
		pageQuery.setQuery(query);
		velocityContext.put("systemAuth", query);
		velocityContext.put("pageInfos",
				SystemTools.convertPaginatedList(this.systemAuthService.queryPageList(pageQuery)));
		String viewName = "system/back/systemAuth-list.vm";
		ViewVelocity.view(request, response, viewName, velocityContext);
	}

	@RequestMapping(value = { "toDetail" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET })
	public void toDetail(@RequestParam(required = true, value = "id") Long id, HttpServletResponse response,
			HttpServletRequest request) throws Exception {
		VelocityContext velocityContext = new VelocityContext();
		String viewName = "system/back/systemAuth-detail.vm";
		SystemAuth systemAuth = this.systemAuthService.queryById(id);
		velocityContext.put("systemAuth", systemAuth);
		ViewVelocity.view(request, response, viewName, velocityContext);
	}

	@RequestMapping(value = { "/toAdd" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET,
			org.springframework.web.bind.annotation.RequestMethod.POST })
	public void toAdd(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws Exception {
		VelocityContext velocityContext = new VelocityContext();
		String viewName = "system/back/systemAuth-add.vm";
		ViewVelocity.view(request, response, viewName, velocityContext);
	}

	@RequestMapping(value = { "/doAdd" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET,
			org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public AjaxJson doAdd(@ModelAttribute SystemAuth systemAuth) {
		AjaxJson j = new AjaxJson();
		try {
			this.systemAuthService.doAdd(systemAuth);
			j.setMsg("保存成功");
		} catch (Exception e) {
			log.info(e.getMessage());
			j.setSuccess(false);
			j.setMsg("保存失败");
		}
		return j;
	}

	@RequestMapping(value = { "toEdit" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET })
	public void toEdit(@RequestParam(required = true, value = "id") Long id, HttpServletResponse response,
			HttpServletRequest request) throws Exception {
		VelocityContext velocityContext = new VelocityContext();
		SystemAuth systemAuth = this.systemAuthService.queryById(id);
		velocityContext.put("systemAuth", systemAuth);
		String viewName = "system/back/systemAuth-edit.vm";
		ViewVelocity.view(request, response, viewName, velocityContext);
	}

	@RequestMapping(value = { "/doEdit" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET,
			org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public AjaxJson doEdit(@ModelAttribute SystemAuth systemAuth) {
		AjaxJson j = new AjaxJson();
		try {
			this.systemAuthService.doEdit(systemAuth);
			j.setMsg("编辑成功");
		} catch (Exception e) {
			log.info(e.getMessage());
			j.setSuccess(false);
			j.setMsg("编辑失败");
		}
		return j;
	}

	@RequestMapping(value = { "doDelete" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET })
	@ResponseBody
	public AjaxJson doDelete(@RequestParam(required = true, value = "id") Long id) {
		AjaxJson j = new AjaxJson();
		try {
			this.systemAuthService.doDelete(id);
			j.setMsg("删除成功");
		} catch (Exception e) {
			log.info(e.getMessage());
			j.setSuccess(false);
			j.setMsg("删除失败");
		}
		return j;
	}

	@RequestMapping(value = { "/initAuth" }, produces = { "text/plain;charset=UTF-8" })
	@ResponseBody
	public String initAuth(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "roleId", required = true) String roleId) {
		log.info("初始化权限");
		String tree = "";
		try {
			List<MenuFunction> allAuthList = this.systemAuthService.queryMenuAndFuncAuth();

			List<MenuFunction> roleAuthList = this.systemAuthService.queryMenuAndFuncAuthByRoleId(roleId);

			tree = SystemUtil.list2TreeWithCheck(allAuthList, roleAuthList);
			log.info("初始化权限: " + tree);
		} catch (Exception e) {
			log.info(e.getMessage());
		}
		return tree;
	}

	@RequestMapping(value = { "/editRoleAuth" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET,
			org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public AjaxJson editRoleAuth(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "checkedNodes", required = true) String checkedNodes,
			@RequestParam(value = "roleId", required = true) String roleId) {
		AjaxJson j = new AjaxJson();
		log.info("编辑角色权限");
		try {
			List<String> authIds = new ArrayList<String>();
			if (StringUtils.isNotEmpty(checkedNodes)) {
				for (String auth : checkedNodes.split(",")) {
					authIds.add(auth);
				}
			}

			this.systemAuthService.modifyOperateRoleAuthRel(roleId, authIds);
			j.setSuccess(true);
			j.setMsg("角色授权成功");
			log.info("编辑角色权限完成 ");
		} catch (Exception e) {
			log.info(e.getMessage());
			j.setSuccess(false);
			j.setMsg("角色授权失败");
		}
		return j;
	}

	@RequestMapping(value = { "/getAuthTree" }, produces = { "text/plain;charset=UTF-8" })
	@ResponseBody
	public String getAuthTree(HttpServletRequest request, HttpServletResponse response) {
		String tree = "";
		try {
			List<MenuFunction> allAuthList = this.systemAuthService.queryMenuAndFuncAuth();
			tree = SystemUtil.listTreeToAuth(allAuthList);
			log.info("权限树: " + tree);
		} catch (Exception e) {
			log.info(e.getMessage());
		}
		return tree;
	}
}

